package com.raining.inningz.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Date;

import com.raining.inningz.database.DatabaseHelper;

import android.content.Context;
import android.database.SQLException;
import android.os.Environment;
import android.text.format.DateFormat;



public class Utils {
	
	
	
	
	
	
	
	
	
	
	   public static String getTimeFormDate(Date date){
	    	
	    	 
	    	DateFormat dateFormat=new DateFormat();
	    	
	    	return dateFormat.format("dd-MM-yy", date).toString();
	    }

	
	public static void checkDatabase(String DB_NAME,Context context) {
		DatabaseHelper myDbHelper = new DatabaseHelper(context,	DB_NAME);
		try {
			myDbHelper.createDataBase(DB_NAME);
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}
		try { 
			myDbHelper.openDataBase(DB_NAME);
			myDbHelper.close();
		} catch (SQLException sqle) {
			throw sqle;
		}
	}
	
	
	public static void exportDatabse(String databaseName,Context context) {
        try {
        	
        	
        	System.out.println("Enter ");
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath = "/data/"+context.getPackageName()+"/databases/"+databaseName+"";
                String backupDBPath = "backupname.db";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                    
                    System.out.println("Done exis");
                }
            }
            
            System.out.println("Done ");
        } catch (Exception e) {
        	System.out.println("EX Done ");
        }
    }
	
}
